// const { rejects } = require("assert");

let indexModule = (function() {
            //权限校验
            function power() {
                let power = localStorage.getItem('power');
                console.log(power)
                if (power === null) {
                    alert("当前操作属于非法进入，请重新登录！", {
                        handle: () => {
                            window.location.href = 'login.html';
                        }
                    });
                    return;
                }
                power = decodeURIComponent(power) //对编码后的URI进行解码
                    //注意：权限校验后，设置权限时最好不要控制元素的显示隐藏，要真正的有或者没有
                let str = `<div class="itemBox">
                    <h3>
                        <i class="iconfont icon-yuangong"></i>
                        员工管理
                    </h3>
                    <nav class="item">
                        <a href="page/userlist.html" target="_iframe">员工列表</a>
                       ${power.includes('userhandle')?` <a href="page/useradd.html" target="_iframe">新增员工</a>`:``}
                    </nav>
                </div>
                <div class="itemBox">
                    <h3>
                        <i class="iconfont icon-guanliyuan"></i>
                        部门管理
                    </h3>
                    <nav class="item">
                        <a href="page/departmentlist.html" target="_iframe">部门列表</a>
                        ${power.includes('departhandle')?` <a href="page/departmentadd.html" target="_iframe">新增部门</a>`:``}
                    </nav>
                </div>
                <div class="itemBox">
                ${power.includes('jobhandle')?` <h3>
                    <i class="iconfont icon-zhiwuguanli"></i>
                    职务管理
                </h3>
                <nav class="item">
                    <a href="page/joblist.html" target="_iframe">职务列表</a>
                    <a href="page/jobadd.html" target="_iframe">新增职务</a>
                </nav>`:``}
                   
                </div>
                <div class="itemBox">
                    <h3>
                        <i class="iconfont icon-kehuguanli"></i>
                        客户管理
                    </h3>
                    <nav class="item">
                        <a href="page/customerlist.html?lx=my" target="_iframe">我的客户</a>
                        ${power.includes('departcustomer') || power.includes('allcustomer')?` <a href="page/customerlist.html" target="_iframe">全部客户</a>`:``}
                        <a href="page/customeradd.html" target="_iframe">新增客户</a>
                    </nav>
                </div> `;
        $('.menuBox').html(str);
    }
    function handle(){
        let $spanName = $(".baseBox").children('span'),
            $menuBox = $(".menuBox"),
            $itemBox = $menuBox.find('.itemBox'),
            $header = $('.headerBox'),
            $baseBox = $header.find('.baseBox'),
            $signoutBtn = $baseBox.children('a'),
            $footer = $('.footerBox'),
            $container = $('.container'),
            $navList = $('.navBox a'),
		    $iframeBox = $('.iframeBox');
            

        // let $signoutBtn = $(".headerBox").find(".baseBox").children('a');
        //验证当前用户是否登录
        axios.get("/user/login").then(result=>{
            if(parseFloat(result.code) === 0){
                //已经登陆过了，需要获取登录用户的基本信息
                return axios.get("/user/info");
            }
            alert('您还未登录，请先进行登录',{
                handle:()=>{
                    window.location.href = 'login.html';
                }
            });
             return Promise.reject();
        }).then(result=>{
            if(parseFloat(result.code) === 0){
                let data = result.data;
                $spanName.html(`欢迎：${data.name}`)
            }
        })
        //安全退出
        $signoutBtn.click(function(){
            axios.get('/user/signout').then(result=>{
                if(parseFloat(result.code) === 0){
                    window.location.href = 'login.html';
                    return;
                }
                alert('当前操作失败，请稍后重试！')
            })
        })
        //实现菜单的折叠功能
        $menuBox.click(function(ev){
            console.log(123)
            let target = ev.target,
                tarTag = target.tagName,
                $target = $(target);
            target === "I" ? ($target = $target.parent(),target = 'H3') : null;
            if(target === 'H3'){
                $target.next().stop().slideToggle(300);
            }
        });
        //点击导航实现menu的切换
        let $organize = $itemBox.filter(':lt(3)'),
		$customer = $itemBox.eq(3),
		initIndex = 0,
		HASH = window.location.href.queryURLParams()['HASH'] || 'organize';
	    HASH === "customer" ? initIndex = 1 : null;

        function change(index){
            $navList.eq(index).addClass('active').siblings().removeClass('active');
            if(index === 0){
                $organize.css('display','block');
                $customer.css('display','none');
                $iframeBox.attr('src','page/userlist.html')

            }else{
                $organize.css('display','none');
                $customer.css('display','block');
                $iframeBox.attr('src','page/customerlist.html?lx=my')
            }
        }
        //默认显示第一个
        change(initIndex);
        //点击那个切换那个
        $navList.click(function(){
            let $this = $(this),
            index = $this.index();
            text = $this.html().trim();
            //权限校验
            if((text === "客户管理" && !/customer/.test(power)) || (text === "组织结构" && !/（userhandle|departhandle|jobhandle）/.test(power))){
                alert("您无权限访问，请先联系管理员");
                return;
            }
            change(index);
        })




    }
    return {
        init(){
            power();
            handle()
        }
    }
})();
indexModule.init();